package com.taskschecker;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.taskschecker.db.DBManager;
import com.taskschecker.db.TaskGroup;

public class TaskGroupInfoActivity extends Activity {
	private DBManager mgr;
	private EditText startDate;
	private EditText endDate;
	private EditText contentText;
	private EditText memoText;
	private Button btnEdit;
	private Button btnStop;
	private List<CheckBox> weekList = new ArrayList<CheckBox>();
	private List<CheckBox> dayList = new ArrayList<CheckBox>();

	private Calendar calToday = Calendar.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_task_group_info);

		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		int screenWidth = metric.widthPixels;

		LinearLayout.LayoutParams layout_params = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		layout_params.width = screenWidth / 2 - 20;

		mgr = new DBManager(this);
		TaskGroup taskgroup = mgr.getTaskGroupById(getIntent().getStringExtra(
				"groupId"));

		startDate = (EditText) findViewById(R.id.EditText_StartDate_Info);
		startDate.setLayoutParams(layout_params);
		String tempStr = (Integer.toString(taskgroup.getStartdate()));
		startDate.setText(tempStr.substring(0, 4) + "-"
				+ tempStr.substring(4, 6) + "-" + tempStr.substring(6, 8));

		endDate = (EditText) findViewById(R.id.EditText_EndDate_Info);
		endDate.setLayoutParams(layout_params);
		tempStr = (Integer.toString(taskgroup.getEnddate()));
		endDate.setText(tempStr.substring(0, 4) + "-" + tempStr.substring(4, 6)
				+ "-" + tempStr.substring(6, 8));

		contentText = (EditText) findViewById(R.id.EditText_Content_Info);
		contentText.setText(taskgroup.getContent());

		memoText = (EditText) findViewById(R.id.EditText_Memo_Info);
		memoText.setText(taskgroup.getMemo());

		btnEdit = (Button) findViewById(R.id.Button_Edit);
		btnEdit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg) {
				Intent intent = new Intent(TaskGroupInfoActivity.this,
						TaskGroupEditActivity.class);
				intent.putExtra("groupId", getIntent()
						.getStringExtra("groupId"));
				startActivity(intent);
				finish();
			}
		});

		btnStop = (Button) findViewById(R.id.Button_Stop);
		btnStop.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						TaskGroupInfoActivity.this);
				builder.setMessage(getResources().getString(
						R.string.alert_stop_task));
				builder.setCancelable(false);
				builder.setPositiveButton("YES",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								mgr.stopTaskGroup(
										getIntent().getStringExtra("groupId"),
										CalendarUtils.getDateInt(calToday));
								sendUpdateWidgetBroadcast();
								finish();
							}
						});
				builder.setNegativeButton("NO",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						});
				builder.create().show();
			}
		});

		if (taskgroup.getRepeatetype() == 0) {
			RadioButton radioButtonDaily = (RadioButton) findViewById(R.id.ataaw1_info);
			radioButtonDaily.setChecked(true);

		} else if (taskgroup.getRepeatetype() == 1) {
			RadioButton radioButtonPerWeek = (RadioButton) findViewById(R.id.ataaw2_info);
			radioButtonPerWeek.setChecked(true);
			createWeekList();
			LinearLayout weekLayout = (LinearLayout) this
					.findViewById(R.id.weekLayout_info);
			weekLayout.setVisibility(View.VISIBLE);
			String sourceStr = taskgroup.getRepeatevalue();
			if (!"".equals(sourceStr)) {
				String[] sourceStrArray = sourceStr.split(",");
				for (int i = 0; i < sourceStrArray.length; i++) {
					weekList.get(Integer.parseInt(sourceStrArray[i]) - 1)
							.setChecked(true);
				}
			}

		} else if (taskgroup.getRepeatetype() == 2) {
			RadioButton radioButtonPerMonth = (RadioButton) findViewById(R.id.ataaw3_info);
			radioButtonPerMonth.setChecked(true);
			createDayList();
			LinearLayout monthLayout = (LinearLayout) this
					.findViewById(R.id.monthLayout_info);
			monthLayout.setVisibility(View.VISIBLE);
			String sourceStr = taskgroup.getRepeatevalue();
			if (!"".equals(sourceStr)) {
				String[] sourceStrArray = sourceStr.split(",");
				for (int i = 0; i < sourceStrArray.length; i++) {
					dayList.get(Integer.parseInt(sourceStrArray[i]) - 1)
							.setChecked(true);
				}
			}
		}
	}

	private void createWeekList() {
		weekList.add((CheckBox) findViewById(R.id.checkBox_week1_info));
		weekList.add((CheckBox) findViewById(R.id.checkBox_week2_info));
		weekList.add((CheckBox) findViewById(R.id.checkBox_week3_info));
		weekList.add((CheckBox) findViewById(R.id.checkBox_week4_info));
		weekList.add((CheckBox) findViewById(R.id.checkBox_week5_info));
		weekList.add((CheckBox) findViewById(R.id.checkBox_week6_info));
		weekList.add((CheckBox) findViewById(R.id.checkBox_week7_info));
	}

	private void createDayList() {
		dayList.add((CheckBox) findViewById(R.id.checkBox_day1_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day2_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day3_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day4_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day5_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day6_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day7_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day8_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day9_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day10_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day11_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day12_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day13_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day14_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day15_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day16_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day17_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day18_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day19_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day20_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day21_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day22_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day23_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day24_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day25_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day26_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day27_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day28_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day29_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day30_info));
		dayList.add((CheckBox) findViewById(R.id.checkBox_day31_info));
	}

	public void sendUpdateWidgetBroadcast() {
		Intent intent = new Intent();
		intent.setAction("cmn.taskschecker.widget.update");
		getApplicationContext().sendBroadcast(intent);
	}
}
